package com.thuanpq60023.easyenglish.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.thuanpq60023.easyenglish.R;

public class MainDisplayActivity extends Activity {

	private static final int LIST_VOCAB = 0;
	private static final int TEST_VOCAB = 1;
	private static final int IMPORT_VOCAB = 2;
	private static final int APP_EXIT = 3;

	private ListView mListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_display);
		initChild();
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		bindListView();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	private void initView() {
		mListView = (ListView) findViewById(R.id.main_list);
	}

	private void initChild() {

	}

	private void bindListView() {
		mListView.setAdapter(new MainDisplayAdapter(this));
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent;
				switch (position) {
				case LIST_VOCAB:
					intent = new Intent(MainDisplayActivity.this,
							ListActivity.class);
					startActivity(intent);
					break;

				case TEST_VOCAB:
					intent = new Intent(MainDisplayActivity.this,
							TestActivity.class);
					startActivity(intent);
					break;

				case IMPORT_VOCAB:
					intent = new Intent(MainDisplayActivity.this,
							ImportActivity.class);
					startActivity(intent);
					break;

				case APP_EXIT:
					finish();
					break;

				default:
					break;
				}
			}
		});
	}

	class MainDisplayAdapter extends BaseAdapter {

		LayoutInflater inflater;
		String[] mItemList;

		public MainDisplayAdapter(Context context) {
			inflater = LayoutInflater.from(context);
			mItemList = getResources().getStringArray(R.array.header_items);
		}

		@Override
		public int getCount() {
			if (mItemList != null) {
				return mItemList.length;
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			return mItemList[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater
						.inflate(R.layout.main_display_item, null);

				holder = new ViewHolder();
				holder.image = (ImageView) convertView.findViewById(R.id.image);
				holder.text = (TextView) convertView.findViewById(R.id.text);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.text.setText(mItemList[position]);

			switch (position) {
			case LIST_VOCAB:
				holder.image.setImageResource(R.drawable.icon);
				break;

			case TEST_VOCAB:
				holder.image.setImageResource(R.drawable.icon);
				break;

			case IMPORT_VOCAB:
				holder.image.setImageResource(R.drawable.icon);
				break;

			case APP_EXIT:
				holder.image.setImageResource(R.drawable.icon);
				break;

			default:
				break;
			}

			return convertView;
		}

		class ViewHolder {
			ImageView image;
			TextView text;
		}
	}

}
